package function;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import entity.Book;
import interfaces.BookDao2;

import java.sql.Connection;
import java.util.List;

public class BookBaseDao2Impl extends BaseDao2<Book> implements BookDao2 {
   private static BookBaseDao2Impl baseDao2=null;
    private BookBaseDao2Impl(){

    }
   public static  BookBaseDao2Impl getBaseDao2(){
        if(baseDao2==null){
            baseDao2=new BookBaseDao2Impl();
        }
    return baseDao2;
    }
    @Override
    public boolean insert(Connection conn, Book book) {
       String sql="insert into book values(?,?,?,?,?,?,?,?)";
        int update = 0;
        try {
            update = update(conn, sql, book.getISBN(), book.getType_id(), book.getBook_name(), book.getAuthor(), book.getPublish(), book.getPublish_date(), book.getNumber(), book.getUnitPrice());
        } catch (MySQLIntegrityConstraintViolationException e) {
            System.out.print("书本名已存在");
        }
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteById(Connection conn, int id) throws MySQLIntegrityConstraintViolationException {
       String sql="delete form book where type_id=?";
        int update = update(conn, sql, id);
        if(update==0)
        {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteByISBN(Connection conn,String ISBN) throws MySQLIntegrityConstraintViolationException {
        String sql="delete from book where ISBN=?";
        int update = update(conn,sql,ISBN);
        if(update==0)
        {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Connection conn, String ISBN, Book book) {
       String sql="update book set ISBN=?,type_id=?,book_name=?,author=?,publish=?,publish_date=?,publish_time=?,unit_price=? where ISBN=?";
        int update = 0;
        try {
            update = update(conn, sql, book.getISBN(), book.getType_id(), book.getBook_name(), book.getAuthor(), book.getPublish(), book.getPublish_date(), book.getNumber(), book.getUnitPrice(),ISBN);
        } catch (MySQLIntegrityConstraintViolationException throwables) {
            System.out.println("书本名已存在");
        }
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public Book getBookByISBN(Connection conn, String ISBN) {
      String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id  where ISBN=?";
        Book book = getInstance(conn, sql, ISBN);
        return book;
    }

    @Override
    public Book getBookTypeByName(Connection conn, String bookName) {
        String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id where book_name=?";
        Book book = getInstance(conn, sql, bookName);
        return book;
    }

    @Override
    public List<Book> getBookByType(Connection conn, int typeId) {
        String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id where type_id=?";
        List<Book> bookArr = getForList(conn, sql, typeId);
        if(bookArr.size()==0){
            return null;
        }
        return bookArr;
    }

    @Override
    public List<Book> getBookByAuthor(Connection conn, String author) {
        String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id where author=?";
        List<Book> bookArr = getForList(conn, sql, author);
        if(bookArr.size()==0){
            return null;
        }
        return bookArr;
    }

    @Override
    public List<Book> getBookByPublish(Connection conn, String publish) {
        String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id where publish=?";
        List<Book> bookArr = getForList(conn, sql, publish);
        if(bookArr.size()==0){
            return null;
        }
        return bookArr;
    }

    @Override
    public List<Book> getAll(Connection conn) {
        String sql="select ISBN ISBN,type_id type_id,type_name type_name,book_name book_name,author author,publish publish,publish_date publish_date,publish_time number,unit_price unitPrice from book left join book_type on type_id=id";
        List<Book> bookArr = getForList(conn, sql);
        if(bookArr.size()==0){
            return null;
        }
        return bookArr;
    }
}
